from LyricEmotion.pyweka.arff import *

class RunWekaError(Exception): pass
class TrainFileNotExistError(RunWekaError):pass
class TestFileNotExistError(RunWekaError):pass
class EssentialParamNotExistError(RunWekaError):pass

def MultiClassClassifierDetail(params):
    """
    @param the trainfile and testfile -t -T >
    # user define 
    -t classtrain.arff
    -T classtest.arff
    > smodisclassresult.txt

    # get from the arff file
    -p should get here

    # default
    -i
    -M
    -distribution
    """
    cmd = "java weka.classifiers.meta.MultiClassClassifier "
    suffix = "-M 0 -R 2.0 -S 1 -W weka.classifiers.functions.Logistic -- -R 1.0E-8 -M -1"
    defaultparams = {"-i":"","-M":"","-distribution":""}

    if (not params.has_key("-t")) or (not params.has_key("-T")) or (not params.has_key(">")):
        raise EssentialParamNotExistError("params not exist error")

    attrs = getAttrList(params["-t"])
    params["-p"] = str(len(attrs))

    for key in defaultparams:
        cmd += " "+key+" "+defaultparams[key]
    
    for key in params:
        if key == ">":
            continue
        cmd += " "+key+" "+params[key]
    cmd += " > "+params[">"]
    cmd += suffix
    
    os.system(cmd)

def MultiClassClassifierNoDetail(params):
    """
    @param the trainfile and testfile -t -T >
    # user define 
    -t classtrain.arff
    -T classtest.arff
    > smodisclassresult.txt

    # default
    -i
    """
    cmd = "java weka.classifiers.meta.MultiClassClassifier "
    suffix = "-M 0 -R 2.0 -S 1 -W weka.classifiers.functions.Logistic -- -R 1.0E-8 -M -1"
    defaultparams = {"-i":"",}

    if (not params.has_key("-t")) or (not params.has_key("-T")) or (not params.has_key(">")):
        raise EssentialParamNotExistError("params not exist error")

    for key in defaultparams:
        cmd += " "+key+" "+defaultparams[key]
    
    for key in params:
        if key == ">":
            continue
        cmd += " "+key+" "+params[key]
    cmd += " > "+params[">"]
    cmd += " "+suffix
    
    os.system(cmd)
